<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
    <title>在线考试系统</title>
    <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script>
    <script type="text/javascript" src="../js/jquery.form.min.js"></script>
    <link type="text/css" href="../css/bootstrap.min.css" rel="stylesheet">
    <link type="text/css" href="../css/main.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-sm-8 col-sm-offset-2" id="login_box">
            <div class="col-sm-5 col-xs-12">
                <h1><span class="text-primary">O</span>EXAM</h1>
                <h1>后台管理&nbsp;<span class="glyphicon glyphicon-tag" aria-hidden="true"></span></h1>

            </div>
            <div class="col-sm-7">
                <form class="form-horizontal" action="user/login.do" method="post" id="login_form">
                    <div class="form-group">
                        <label for="input_email" class="col-sm-4 col-xs-12 control-label">邮箱：</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="email" class="form-control" id="input_email" placeholder="请输入邮箱地址"
                                   name="email">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="input_password" class="col-sm-4 col-xs-12 control-label">密码：</label>
                        <div class="col-sm-8 col-xs-12">
                            <input type="password" class="form-control" id="input_password" placeholder="请输入密码"
                                   name="password">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <button type="submit"
                                    class="btn btn-default btn-primary col-sm-6 col-sm-offset-5 col-xs-12" id="btn-login" data-loading-text="登陆中...">登陆
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>

    $.get("../user/info.do", function (data) {

        if (data.error == 0) {
            var user = data.data;
            if (user.role == 2) {
                window.open("manager.html", "_self");
            } else {
                alert("非管理员用户,请更换管理员用户登陆");
                var flag = confirm("是否退出当前用户并重新登陆？");
                if (flag) {
                    $.get("../user/logout.do", function (data) {
                        if (data.error != 0) {
                            alert(data.msg);
                        }
                    });
                }else {
                    window.open("../user/exam.html", "_self");
                }

            }
        }
    });

    $("#login_form").validate({
        submitHandler: function (form) {
            $("#btn-login").button('loading');
            $(form).ajaxSubmit({
                dataType: "json",
                success: function (data) {
                    // 解析数据时报错，原因是返回的数据已经是object格式，无需再使用var result = JSON.parse(data); 进行转换，
                    // 直接使用var result =data.data；
                    if (data.error == 0) {
                        window.open("manager.html", "_self");
                    } else {
                        alert(data.msg);
                        $("#btn-login").button('reset');
                    }
                }
            });

        },
        errorClass: "text-danger",
        errorElement: "span",
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 18
            }

        },
        messages: {
            email: {
                required: "请输入邮箱地址",
                email: "邮箱格式不正确"
            },
            password: {
                required: "请输入密码",
                minlength:
                    "密码长度不能小于 6 个字符",
                maxlength:
                    "密码长度不能超过 18 个字符"
            }
        }

    })

</script>
</html>